package web.controller;

import config.SpringContextUtil;
import entity.Department;
import entity.Instructor;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import service.InstructorService;
import service.StudentService;
import service.UserService;
import serviceImpl.InstructorServiceImpl;
import serviceImpl.StudentServiceImpl;
import serviceImpl.UserServiceImpl;

import java.io.IOException;
import java.util.List;

@WebServlet(name = "instructor", urlPatterns = "/instructor")
public class InstructorServlet extends HttpServlet {
    private final String instructor_list_page = "/WEB-INF/views/user/user_instructor_list.jsp";
    private final String instructor_viewEdit_page = "/WEB-INF/views/user/user_instructor_edit.jsp";
    private final String instructor_add_page = "/WEB-INF/views/user/user_instructor_add.jsp";
    @Autowired
    private InstructorService instructorService;

    @Override
    public void init() throws ServletException {
        try {
            this.instructorService = (InstructorServiceImpl) SpringContextUtil.getBean("instructorServiceImpl", InstructorService.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if (action == null) {
            List<Instructor> instructors = instructorService.getAllInstructor();
            req.setAttribute("instructors", instructors);
            RequestDispatcher dispatcher = req.getRequestDispatcher(instructor_list_page);
            dispatcher.forward(req, resp);
        } else if (action.equals("viewEdit")) {
            String instID = req.getParameter("instID");

            //instID该属性值在后续修改信息需要用到
            req.getSession().setAttribute("instID", instID);
            Instructor instructor = instructorService.getInstructorByInstID(instID);
            req.setAttribute("instructor", instructor);
            RequestDispatcher dispatcher = req.getRequestDispatcher(instructor_viewEdit_page);
            dispatcher.forward(req, resp);

        } else if (action.equals("edit")) {
            String instID = req.getSession().getAttribute("instID").toString();
            String instName = req.getParameter("instName");
            String telephone = req.getParameter("telephone");
            String password = req.getParameter("password");
            Instructor instructor = new Instructor(instID, instName, telephone, password);
            boolean flag = instructorService.updateInstructor(instructor);
            if (flag) {
                String message = "修改成功!";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');window.location.href='" + req.getContextPath() + "/instructor';</script>";
                resp.getWriter().write(jsCode);
            }
        } else if (action.equals("delete")) {
            String instID = req.getParameter("instID");
            boolean flag = instructorService.deleteInstructor(instID);
            if (flag) {
                String message = "删除成功!";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');window.location.href='" + req.getContextPath() + "/instructor';</script>";
                resp.getWriter().write(jsCode);
            }
        } else if (action.equals("viewAdd")) {
            RequestDispatcher dispatcher = req.getRequestDispatcher(instructor_add_page);
            dispatcher.forward(req, resp);
        } else if (action.equals("add")) {
            String instID = req.getParameter("instID");
            String instName = req.getParameter("instName");
            String depID = req.getParameter("depID");
            String telephone = req.getParameter("telephone");
            String password = req.getParameter("password");
            Instructor instructor = new Instructor(instID, instName, depID, telephone, password);
            boolean flag = instructorService.addInstructor(instructor);
            if (flag) {
                String message = "添加成功!";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');window.location.href='" + req.getContextPath() + "/instructor';</script>";
                resp.getWriter().write(jsCode);
            } else {
                String message = "添加失败";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');</script>";
                resp.getWriter().write(jsCode);
            }
        }else if (action.equals("search")) {
            String keyword = req.getParameter("keyword");
            List<Instructor> instructors = instructorService.getInstructorByInstIDORInstName(keyword,keyword);
            req.setAttribute("instructors", instructors);
            RequestDispatcher dispatcher = req.getRequestDispatcher(instructor_list_page);
            dispatcher.forward(req, resp);
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
